package View.Repair;

import Service.RepairService;
import Service.ServiceImpl.RepairServiceImpl;
import View.Index.Index_admin;
import domain.Repair;
import domain.User;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateRepair extends JFrame {
    public UpdateRepair(User user){
        init();
        componentInit(user);
    }

    public void init(){
        this.setTitle("修改报修");
        this.setSize(488,474);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(null);
    }
    public void componentInit(User user) {
        //------------------JLabel--------------------
        JLabel label1 = new JLabel();
        label1.setText("请输入报修号:");
        label1.setBounds(74, 14, 87, 30);
        JLabel label2 = new JLabel();
        label2.setText("查询结果如下:");
        label2.setBounds(188, 55, 126, 63);
        JLabel label3 = new JLabel();
        label3.setText("报修号:");
        label3.setBounds(144, 132, 65, 30);
        JLabel label4 = new JLabel();
        label4.setText("宿舍号:");
        label4.setBounds(145, 183, 65, 30);
        JLabel label5 = new JLabel();
        label5.setText("问题:");
        label5.setBounds(147, 228, 65, 30);
        JLabel label6 = new JLabel();
        label6.setText("描述:");
        label6.setBounds(147, 274, 65, 30);
        JLabel label7 = new JLabel();
        label7.setText("是否完成修理:");
        label7.setBounds(120, 323, 91, 30);

        //------------------JTextField--------------------
        JTextField field1 = new JTextField();
        field1.setText("");
        field1.setBorder(new LineBorder(Color.lightGray));
        field1.setBounds(182, 12, 120, 30);
        JTextField field2 = new JTextField();
        field2.setEnabled(false);
        field2.setText("");
        field2.setBorder(new LineBorder(Color.lightGray));
        field2.setBounds(222, 136, 120, 30);
        JTextField field3 = new JTextField();
        field3.setText("");
        field3.setBorder(new LineBorder(Color.lightGray));
        field3.setBounds(223, 184, 120, 30);
        JTextField field4 = new JTextField();
        field4.setText("");
        field4.setBorder(new LineBorder(Color.lightGray));
        field4.setBounds(225, 229, 120, 30);
        JTextField field5 = new JTextField();
        field5.setText("");
        field5.setBorder(new LineBorder(Color.lightGray));
        field5.setBounds(227, 277, 120, 30);
        JTextField field6 = new JTextField();
        field6.setText("");
        field6.setBorder(new LineBorder(Color.lightGray));
        field6.setBounds(228, 320, 120, 30);

        //------------------JButton--------------------
        JButton button1 = new JButton();
        button1.setText("查询");
        button1.setBounds(321, 15, 95, 27);
        JButton button2 = new JButton();
        button2.setText("修改");
        button2.setEnabled(false);
        button2.setBounds(95, 377, 95, 27);
        JButton button3 = new JButton();
        button3.setText("退出");
        button3.setBounds(289, 378, 95, 27);


        this.add(label1);
        this.add(label2);
        this.add(label3);
        this.add(label4);
        this.add(label5);
        this.add(label6);
        this.add(label7);
        this.add(field1);
        this.add(field2);
        this.add(field3);
        this.add(field4);
        this.add(field5);
        this.add(field6);
        this.add(button1);
        this.add(button2);
        this.add(button3);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = field1.getText();
                RepairService service = new RepairServiceImpl();
                Repair repair = service.selectRepairById(id);

                field2.setText(String.valueOf(repair.getId()));
                field3.setText(repair.getDormnum());
                field4.setText(repair.getProblem());
                field5.setText(repair.getDescription());
                field6.setText(repair.getIsfinish());

                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"查询结果如下,请检查后再修改:");
                button2.setEnabled(true);
            }
        });

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Repair repair = new Repair();
                repair.setId(Integer.parseInt(field1.getText()));
                repair.setDormnum(field3.getText());
                repair.setProblem(field4.getText());
                repair.setDescription(field5.getText());
                repair.setIsfinish(field6.getText());

                RepairService service = new RepairServiceImpl();
                service.updateRepair(repair);
                JFrame frame = new JFrame();
                JOptionPane.showMessageDialog(frame,"修改成功！");
                field1.setText("");
                field2.setText("");
                field3.setText("");
                field4.setText("");
                field5.setText("");
                field6.setText("");
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
                new Index_admin(user).setVisible(true);
            }
        });
    }
}
